public  with sharing class customControllerCalendarArea {

//propiedades de la clase
public Personal_Time_Off__c p{get; set;}

public string Store{get;set;}  

@RemoteAction
public static Account getUserStore(string userId){

User user=[select AccountId from User where Id =:userId.substring(0,15)];
Account account =[select Current_Store__r.Name, Current_Store__c,Area__c,id  from Account where id=:user.AccountId];

return account;
}

@RemoteAction
public static list<Personal_Time_Off__c> createTimeOffRequest(string accid,double days,string startdate,string vtype,string ptoname,string accountid){
Personal_Time_Off__c pto = new Personal_Time_Off__c(); 
list<string> approvers = new list<string>();
User currentUser;
list<Personal_Time_Off__c> ptos = new list<Personal_Time_Off__c>(); 
pto.Name=ptoname;
pto.Employee__c = accid;
pto.Number_Days__c=days;
pto.Date__c=Date.parse(startdate);
pto.Type__c=vtype;
database.insert(pto);

//approval process

Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
 req.setComments('Submitting request for approval of.'+pto.Name);
 req.setObjectId(pto.id);
 currentUser=[Select u.Manager.Id, u.ManagerId From User u Where Id =: UserInfo.getUserId()];
 approvers.add(currentUser.Id);
 req.setNextApproverIds(approvers);
 Approval.ProcessResult result = Approval.process(req);
ptos = [Select Id,Employee__r.FirstName,Employee__r.LastName,Name,Date__c,EndDate__c,Number_Days__c,Approved__c From Personal_Time_Off__c Where Employee__r.id= :accid and Employee__r.Id=:accountid.substring(0,15) ];
 return ptos;
}


@RemoteAction
public static list<Personal_Time_Off__c>  timeOffArea(string AreaId){
        list<Personal_Time_Off__c> pto;
        pto = [Select Id,Employee__r.FirstName,Employee__r.LastName,Name,Date__c,EndDate__c,Number_Days__c,Approved__c From Personal_Time_Off__c Where Employee__r.Area__c= :AreaId.substring(0,15)];
        return pto;
}

@RemoteAction
public static list<Personal_Time_Off__c>  timeOffAreaUser(string AreaId,string userid){
        list<Personal_Time_Off__c> pto;
        pto = [Select Id,Employee__r.FirstName,Employee__r.LastName,Name,Date__c,EndDate__c,Number_Days__c,Approved__c From Personal_Time_Off__c Where Employee__r.Area__c= :AreaId.substring(0,15) and Employee__r.Id=:userid.substring(0,15)];
        return pto;
}


@RemoteAction
public static list<Personal_Time_Off__c>  timeOffAreaDate(string AreaId,string DateVar){
        list<Personal_Time_Off__c> pto;    
        pto = [Select Id,Employee__r.FirstName,Employee__r.LastName,Name,Date__c,EndDate__c,Number_Days__c,Approved__c From Personal_Time_Off__c Where Employee__r.Area__c= :AreaId.substring(0,15) and Date__c <= :Date.parse(DateVar) and EndDate__c >=:Date.parse(DateVar)];
        return pto;
}

@RemoteAction
public static list<Area__c> GetAreas(){
        list<Area__c> Areas;
        Areas =[Select Id,Name From Area__c]; 
    return Areas;
}

@RemoteAction
public static void setApprovePTO(string ptoid){
 Personal_Time_Off__c pto =[Select Id,Approved__c from Personal_Time_Off__c where Id=:ptoid];
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Approving request pto');
        req.setAction('Approve');
        req.setWorkitemId(ptoid);
        Approval.ProcessResult result =  Approval.process(req);
        if(result.isSuccess()){
       pto.approved__c = true; 
        database.update(pto);
       
        
        }
           
}



}